import { bodyConfigs, calcBodyPart, createBodyGetter, specialBodyConfig } from "@/role/bodyUtils"
import { CreepConfig, CreepRole, RoleCreep, RoleDatas } from "@/role/types/role"
import { getBodySpawnEnergy } from "@/utils"
import { taskManager } from "@/app/creep/roles/taskManager"
import { harvester, manager, worker } from "./roles"


export type CreepWork = {
    [Role in CreepRole]: CreepConfig<Role>
}


const processor: CreepConfig<CreepRole.Processor> = {
    target: (creep: RoleCreep) => {
        return true
    },
    bodys: (room: Room, spawn: StructureSpawn, data: RoleDatas[CreepRole.Processor]) => {
        const bodyConfig = bodyConfigs.manager
        const bodyPartGenerator = createBodyGetter(bodyConfig)(room, spawn)
        console.log(room.name, 'processor manager bodyPartGenerator', bodyPartGenerator, getBodySpawnEnergy(bodyPartGenerator))
        // if (room.controller.level < 6) {
        //     return calcBodyPart([[CARRY, 2], [MOVE, 1]])
        // }
        // else if (room.controller.level < 6) {
        //     return calcBodyPart([[CARRY, 4], [MOVE, 2]])
        // }
        // else if (room.controller.level < 7) {
        //     return calcBodyPart([[CARRY, 8], [MOVE, 4]])
        // }
        // return calcBodyPart([[CARRY, 14], [MOVE, 7]])
        return bodyPartGenerator
    }
}


const upgrader: CreepConfig<CreepRole.Upgrader> = {
    target: (creep: RoleCreep) => {
        return true
    },
    bodys: (room: Room, spawn: StructureSpawn, data: RoleDatas[CreepRole.Upgrader]) => {

        const bodyConfig = bodyConfigs.worker
        let bodyPartGenerator
        if (data.bodyType) {
            // 指定了特殊身体部件的话就生成对应的
            bodyPartGenerator = specialBodyConfig[data.bodyType](room, spawn)
        }
        else {
            // 否则就使用默认的身体部件
            bodyPartGenerator = createBodyGetter(bodyConfig)(room, spawn)
        }
        console.log(room.name, 'worker bodyPartGenerator', bodyPartGenerator, getBodySpawnEnergy(bodyPartGenerator))
        return bodyPartGenerator
    }
}


export const roles: CreepWork = {
    //
    taskManager,
    //
    harvester, manager, worker, processor, upgrader,
    //
    remoteManager: {
        target: (creep: RoleCreep) => {
            return true
        },
        bodys: (room: Room, spawn: StructureSpawn, data: RoleDatas[CreepRole]) => {
            const bodyConfig = bodyConfigs.remoteManager
            const bodyPartGenerator = createBodyGetter(bodyConfig)(room, spawn)
            console.log(room.name, 'remoteManager bodyPartGenerator', bodyPartGenerator, getBodySpawnEnergy(bodyPartGenerator))
            return bodyPartGenerator
        }
    },
    remoteHarvester: {
        target: (creep: RoleCreep) => {
            return true
        },
        bodys: (room: Room, spawn: StructureSpawn, data: RoleDatas[CreepRole]) => {
            const bodyConfig = bodyConfigs.harvester
            const bodyPartGenerator = createBodyGetter(bodyConfig)(room, spawn)
            console.log(room.name, 'remote harvester bodyPartGenerator', bodyPartGenerator, getBodySpawnEnergy(bodyPartGenerator))
            if (room.controller.level < 7) {
                return calcBodyPart([[WORK, 4], [CARRY, 1], [MOVE, 4]])
            }
            return calcBodyPart([[WORK, 8], [CARRY, 1], [MOVE, 8]])
        }
    },
    wallerFiller: {
        target: (creep: RoleCreep) => {
            return true
        },
        bodys: (room: Room, spawn: StructureSpawn, data: RoleDatas[CreepRole]) => {
            return calcBodyPart([[WORK, 9], [CARRY, 9], [MOVE, 9]])
        }
    },
    claimer: {
        target: (creep: RoleCreep) => {
            return true
        },
        bodys: (room: Room, spawn: StructureSpawn, data: RoleDatas[CreepRole]) => {
            return calcBodyPart([[CLAIM, 2], [MOVE, 1]])
        }
    },
    reserver: {
        target: (creep: RoleCreep) => {
            return true
        },
        bodys: (room: Room, spawn: StructureSpawn, data: RoleDatas[CreepRole]) => {
            return calcBodyPart([[CLAIM, 2], [MOVE, 1]])
        }
    },
    centerManger: {
        target: (creep: RoleCreep) => {
            return true
        },
        bodys: (room: Room, spawn: StructureSpawn, data: RoleDatas[CreepRole]) => {
            const bodyConfig = bodyConfigs.processor
            const bodyPartGenerator = createBodyGetter(bodyConfig)(room, spawn)
            console.log(room.name, 'centerManger processor bodyPartGenerator', bodyPartGenerator, getBodySpawnEnergy(bodyPartGenerator))
            return bodyPartGenerator
        }
    },
    remoteWorker: {
        target: (creep: RoleCreep) => {
            return true
        },
        bodys: (room: Room, spawn: StructureSpawn, data: RoleDatas[CreepRole]) => {
            return calcBodyPart([[WORK, 6], [CARRY, 4], [MOVE, 8]])
        }
    },
    attacker: {
        target: (creep: RoleCreep) => {
            return true
        },
        bodys: (room: Room, spawn: StructureSpawn, data: RoleDatas[CreepRole]) => {
            return calcBodyPart([[TOUGH, 10], [RANGED_ATTACK, 2], [MOVE, 7]])
        }
    },
    centerLabManager: {
        target: (creep: RoleCreep) => {
            return true
        },
        bodys: (room: Room, spawn: StructureSpawn, data: RoleDatas[CreepRole]) => {
            return calcBodyPart([[CARRY, 10], [MOVE, 5]])
        }
    }
}
